//Cracking the coding interview (5th), 2_3
//Implement an algorithm to delete a node in the middle of a singly linked list, given only access to that node.
//EXAMPLE
//Imput: the node c from the linked list a->b->c->d->e
//Result: nothing is returned, but the new linked list looks like a->b->d->e
//7:53PM-8:03PM-8:08PM(bugfree)

#include <iostream>
using namespace std;

struct node {
  node() : c(0), next(NULL) {}
  char c;
  struct node *next;
};

void list_del(struct node *n) 
{
  struct node *p = n;
  if (!n)
    return;

  n->c = n->next->c;
  n->next = n->next->next;
}

int main(int argc, char *argv[])
{
  struct node *list = new node[5];
  struct node *n = &list[2];

  list[0].c = 'a';
  list[1].c = 'b';
  list[2].c = 'c';
  list[3].c = 'd';
  list[4].c = 'e';

  for (int i = 0; i < 4; i++) {
    list[i].next = &list[i+1];
  }
  list_del(n);
  while(list) {
    cout << list->c; 
    if (list->next)
      cout << "->";
    list = list->next;
  }
  cout << endl;
  
  delete[] list;
  return 0;
}
